<template>
	<view>
		<view class="dizhi-box">
		<view class="item" v-for="(item,i) in dizhi" :key="item.adId">
			<view class="name-box">
				<view class="names">{{item.adName}}</view>
				<view class="tel">{{item.adPhone}}</view>
			</view>
			<view class="dizhi">{{item.adDetail}}</view>
			<view class="name-box">
				<label>
					<checkbox :checked="item.adStatus==1" @click="goSetDefault(item.adId,i)" /><text>默认</text>
				</label>
				<view class="bianji-box">
					<view class="edit-box" @click="goEdit(item.adId,item.adName,item.adPhone,item.adDetail,item.area.arName,item.area.city2.ctName,item.area.city2.provice.prName,item.arId)">
						<image src="../../static/icons/bianji.png" mode="widthFix" class="icons"></image>
						<text>编辑</text>
					</view>
					<view class="del-box" @click="goDelete(item.adId,i)">
						<image src="../../static/icons/delete.png" mode="widthFix" class="icons"></image>
						<text>删除</text>
					</view>
					
				</view>
			</view>
		</view>
		</view>
		<view class="fixbottom">
			<button type="default" class="add-box" @click="addNewDizhi()">添加新地址</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				dizhi:[],
				oldDft:"",	// 原默认收货地址id
				newDft:"",	// 新默认收货地址id
				from:""	// 页面来源方式
			}
		},
		methods: {
			// 删除地址
			goDelete(id,n){
				uni.showModal({
					title:"删除地址?",
					success: (res) => {
						if(res.confirm){
							this.$http.get('/appaddress.do', {
								p: 'deleteAddress',
								id:id
							}).then(res => {
								if(res.data=='success'){
									this.$u.toast("删除成功");
									this.dizhi.splice(n,1)
								}
							})
						}
					}
				})
			},
			// 添加新地址
			addNewDizhi(){
				uni.navigateTo({
					url:'addUpdateDizhi?type=add&adid='+this.oldDft
				})
			},
			// 设置默认地址
			goSetDefault(id,n){
				// appaddress.do?p=updataStatus&id=114&adId=115
				this.$http.get('/appaddress.do', {
					p: 'updataStatus',
					id:id,
					adId:this.oldDft,
					status:1
				}).then(res => {
					if(res.data=='1'){
						for(let i=0;i<this.dizhi.length;i++){
							this.dizhi[i].adStatus=0;
						}
						this.dizhi[n].adStatus=1;
						this.oldDft = id;
						this.$u.toast("设置成功");
						if(this.from==2){	// 返回到下单页面
							setTimeout(function(){
								uni.$emit('getDizhi');
								uni.navigateBack({
									
								})
							},500)
						}else if(this.from==3){	// 返回立即购买
							setTimeout(function(){
								uni.$emit("getDizhiFromLjgm");
								uni.navigateBack({
									
								})
							},500)
						}
					}
				})
			},
			// 点击编辑地址
			goEdit(id,n,t,d,a,c,p,arId){//参数含义： 当前地址id，姓名，电话，详情，区县，市，省
				uni.navigateTo({
					url:'addUpdateDizhi?type=update&adid='+this.oldDft+"&adId="+id+"&adname="+n+"&adtel="+t+"&addetail="+d+"&aname="+a+"&cname="+c+"&pname="+p+"&arId="+arId
				})
			},
			// 获取当前用户的收货地址
			getDizhi(){
				// /appaddress.do/getAllAddress
				this.$http.get('/appaddress.do', {
					p: 'getAllAddressxcx',
					id:uni.getStorageSync("usersId")
				}).then(res => {
					if(res.data.code==200){
						this.dizhi = res.data.data;
						for(let i=0,len=res.data.data.length;i<len;i++){
							if(res.data.data[i].adStatus==1){
								this.oldDft = res.data.data[i].adId;
								break;
							}
						}
					}else{
						this.$u.toast("请先登录");
					}
				})
			}
		},
		onLoad(opt) {
			if(opt.from==1){		// from=1 从个人中心进入的  from=2  从下单页面进入的
				this.from = 1;
			}else if(opt.from == 2)
			{
				this.from = 2;
			}else if(opt.from==3){
				this.from = 3;
			}
			this.getDizhi();
			uni.$on("getDizhi",this.getDizhi);
		},
	}
</script>

<style scoped>
	.dizhi-box{
		padding-bottom: 100rpx;
	}
	.item{
		padding: 10rpx 20rpx;
		background-color: white;
		margin-bottom: 10rpx;
	}
	.item .dizhi{
		padding: 10rpx 20rpx;
		border-bottom: 1px solid #F1F1F1;
	}
 .name-box{
	 display: flex;
	 padding: 10rpx 20rpx;
	 justify-content: space-between;
 }
 .name-box .icons{
	 width: 40rpx;
	 vertical-align: middle;
	 margin:0rpx 10rpx ;
 }
 .bianji-box{
	 display: flex;
 }
 .fixbottom{
	 position: fixed;
	 width: 100%;
	 height: 100rpx;
	 bottom: -10rpx;
	 left: 0rpx;
 }
 .add-box{
	 background-color: #FF6700;
	 color: white;
 }
</style>
